package com.unitd.base.frame.plugin.module.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 前段控制器基类
 *
 * @author Hudan
 * @version 1.0
 * @filename BaseController.java
 * @copyright www.unitd.com
 * @date 2019-01-23
 */
@RequestMapping(value = "/v1/shop", produces = {"application/json;charset=UTF-8"})
@Slf4j
public class BaseController {

    /**
     * 调用spring的校验, 反馈错误消息
     *
     * @param bindingResult 自动绑定结果
     * @return 错误描述信息
     */
    protected String getErrorMsg(BindingResult bindingResult) {
        // 拿到所有异常信息
        List<ObjectError> errorList = bindingResult.getAllErrors();

        // 默认只返回第一个,让前端看到一个错误即可
        return errorList.get(0).getDefaultMessage();
    }

    /**
     * 获取 refererUrl
     *
     * @param request http请求
     * @return refererUrl
     */
    protected String getRefererUrl(HttpServletRequest request) {
        return request.getHeader("referer");
    }
}